<?php

class DeliveryUpdateWidget extends CWidget {

    public $visible = true;

    public function init() {
        
    }

    public function run() {
        if ($this->visible) {
            $this->renderContent();
        }
    }

    protected function renderContent() {
        $lCountry = Countries::model()->findAll();
        $listcountries = array();
        if ($lCountry && count($lCountry) > 0) {
            $listcountries = CHtml::listData($lCountry, 'countryCode', 'countryName');
        }

        $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
        $model = DeliveryMethod::model()->findByPk($id);
        // if it is ajax validation request
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'sanpham-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
        // collect user input data
        if (isset($_POST['DeliveryMethod'])) {
            $model->attributes = $_POST['DeliveryMethod'];
            if ($model->save()) {
                user()->setFlash('success', t('cms', 'Update Successfully!'));
            }
        }

        $this->render('cmswidgets.views.shopping.delivery_create_widget', array('model' => $model, 'listcountries' => $listcountries, 'act' => 'update'));
    }

}
